<!DOCTYPE html>
<html lang="en" >
<head>
    <meta charset="UTF-8">
    <title>商品详情</title>

    <script type="text/javascript" src="/bootstrap-3.3.7-dist/js/bootstrap.js"></script>

    <script type="text/javascript" src="/bootstrap-3.3.7-dist/css/bootstrap.css"></script>

    <script type="text/javascript" src="/js/jquery-1.12.4.min.js"></script>

    <script type="text/javascript" src="/js/jquery.md5.js"></script>

    <script type="text/javascript" src="/js/common.js"></script>

</head>
<body>


    <div>秒杀商品详情</div>

    <div>
        <span id="userTip">您还没有登录，请登陆后操作<br/></span>
        <span>没有收货地址的提示</span>
    </div>

    <table class="table" id="goodsList">

        <tr>
            <td>商品名称</td>
            <td id="goodsName"></td>
        </tr>

        <tr>
            <td>商品图片</td>
            <td><img id="goodsImg" width="100" height="100"/></td>
        </tr>

        <tr>
            <td>秒杀开始时间</td>
            <td id="startDate"></td>

            <td>
                <input type="hidden" id="remainSeconds">
               <span id="miaoshaTip"></span>
            </td>

            <td>
<!--            <form id="miaoshaForm" method="post" action="/miaosha/do_miaosha">-->
<!--                <button class="btn btn-primary btn-block" type="submit" id="butButton">立即秒杀</button>-->
<!--                <input type="hidden" id="goodsId" name="goodsId">-->
<!--            </form>-->

                <div class="row">
                    <div class="form-inline">

                        <img id="verifyCodeImg" width="80" height="32" style="display: none" onclick="refreshVerifyCode()"/>
                        <input id="verifyCode" class="form-control" style="display: none"/>
                        <button class="btn btn-primary" type="button" id="butButton" onclick="getMiaoshaPath()">立即秒杀</button>

                    </div>
                </div>
                    <input type="hidden" id="goodsId" name="goodsId">

            </td>
        </tr>

        <tr>
            <td>商品原价</td>
            <td id="goodsPrice"></td>
        </tr>

        <tr>
            <td>商品秒杀价</td>
            <td id="miaoshaPrice" ></td>
        </tr>

        <tr>
            <td>库存数量</td>
            <td id="stockCount"></td>
        </tr>

    </table>

</body>

<script>

    //5 客户端轮询，是否秒杀成功
    function getMiaoshaResult(goodsId) {
        $.ajax({
            url:"/miaosha/result",
            data: {
                goodsId: goodsId
            },
            type:"get",
            success:function (data) {

                // alert(data.code)

                if (data.code == 0){

                    var result = data.data;
                    if (result < 0 ){
                        alert("对不起，秒杀失败");
                    }else if (result == 0){   //继续轮询
                        setTimeout(function () {
                            getMiaoshaResult(goodsId);
                        },200);
                    }else{
                        alert("恭喜你，秒杀成功! ");
                        window.location.href = "/order_detail.htm?orderId=" + result;
                    }
                }else{
                    alert(data.msg);
                }
            },
            error:function () {
                alert("秒杀失败")
            }
        })
    }

//秒杀开始之前，先去请求接口获取秒杀地址
    function getMiaoshaPath(){

        var goodsId = $("#goodsId").val();

        $.ajax({
            url:"/miaosha/path",
            data: {
                goodsId: goodsId,
                verifyCode: $("#verifyCode").val()
            },
            type:"get",
            success:function (data) {

                if (data.code == 0){

                    var path = data.data

                    doMiaosha(path);

                }else{
                    alert(data.msg);
                }
            },
            error:function () {
                alert("秒杀失败")
            }
        })
    }


    function doMiaosha(path){

        $.ajax({
            url:"/miaosha/" + path + "/do_miaosha",
            data: {
                goodsId: $("#goodsId").val()
            },
            type:"post",
            success:function (data) {

                if (data.code == 0){
                    //跳转到订单详情
                    // window.location.href = "/order_detail.htm?orderId=" + data.data.id;

                    //5 客户端轮询，是否秒杀成功
                    getMiaoshaResult($("#goodsId").val());

                }else{
                    alert(data.msg);
                }
            },
            error:function () {
                alert("秒杀失败")
            }
        })
    }


    $(function () {

        getDetail();

    });


    function getDetail() {

        var goodsId = g_getQueryString("goodsId");   //从传过来的路由中取到值

        $.ajax({
            url:"/goods/detail/" + goodsId,
            type:"get",
            success:function (data) {
                if (data.code == 0){

                    render(data.data);
                }else{
                    alert(data.msg);
                }

            },
            error:function () {
                alert("客户端请求错误")
            }
        })
    }
    
    function render(detail) {

         var miaoshaStatus = detail.miaoshaStatus;
         var remainSeconds = detail.remainSeconds;
         var goods = detail.goods;
         var user = detail.user;

         if (user){
             $("#userTip").hide();
         }
         $("#goodsName").text(goods.goodsName);
         $("#goodsImg").attr("src",goods.goodsImg);
         $("#startDate").text(new Date(goods.startDate).format("yyyy-MM-dd hh:mm:ss"));
         $("#remainSeconds").val(remainSeconds);
         $("#goodsId").val(goods.id);
         $("#goodsPrice").text(goods.goodsPrice);
         $("#miaoshaPrice").text(goods.miaoshaPrice);
         $("#stockCount").text(goods.stockCount);
         countDown();
    }

    function countDown() {

        var remainSeconds = $("#remainSeconds").val();
        var timeout;   //定时器

        if (remainSeconds > 0) {   //秒杀没开始，倒计时

            $("#butButton").attr("disabled",true);

            $("#miaoshaTip").html("秒杀倒计时: " + remainSeconds + "秒")

            timeout = setTimeout(function () {

                $("#countDown").text(remainSeconds - 1);
                $("#remainSeconds").val(remainSeconds - 1);
                countDown();

            },1000);

        }else if (remainSeconds == 0){  //秒杀正在进行中

            $("#butButton").attr("disabled",false);

            if (timeout){
                clearTimeout(timeout);   //清除定时器
            }
            $("#miaoshaTip").html("秒杀正在进行中")

            //生成验证码
            $("#verifyCodeImg").attr("src","/miaosha/verifyCode?goodsId=" +$("#goodsId").val());
            $("#verifyCodeImg").show();
            $("#verifyCode").show();

        }else{      //秒杀结束

            $("#butButton").attr("disabled",true);
            $("#miaoshaTip").html("秒杀结束")
            $("#verifyCodeImg").hide();
            $("#verifyCode").hide();
        }
    }
    
    function refreshVerifyCode() {
        $("#verifyCodeImg").attr("src","/miaosha/verifyCode?goodsId=" +$("#goodsId").val() +"&timestamp=" + new Date().getTime());
    }





</script>










</html>